package com.company.educenter.controller;


import com.company.educenter.entity.UcenterMember;
import com.company.educenter.service.UcenterMemberService;
import com.company.educenter.vo.RegistVo;
import com.company.servicebase.to.order.UserTo;
import com.company.utils.JwtUtils;
import com.company.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-21
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Resource
    private UcenterMemberService memberService;

    //用户登录
    @PostMapping("/login")
    public R loginUser(@Validated @RequestBody UcenterMember user) {
        String token = memberService.login(user);
        return R.ok().data("token", token);
    }

    //用户注册
    @PostMapping("/register")
    public R register(@RequestBody RegistVo registVo) {
        memberService.register(registVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);

    }

    //根据用户ID查询用户(限作用于订单生产所需)
    @GetMapping("/getUser/{id}")
    public UserTo getUserById(@PathVariable("id") String id) {
        UcenterMember member = memberService.getById(id);
        UserTo userTo = new UserTo();
        BeanUtils.copyProperties(member, userTo);
        return userTo;
    }

    //查询一天的注册人数
    @GetMapping("/countRegister/{day}")
    public Integer getRegisterCount(@PathVariable("day") String day) {
        Integer count = memberService.getRegisterCount(day);
        return count;
    }


}

